package Presentation;

import BusinessLogic.Command;
import BusinessLogic.Connection;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import java.awt.event.*;

/**
 *
 * @author 4733Z
 */
public class frmChangePass extends javax.swing.JFrame {
    
    public String username;
    public frmChangePass() {
        initComponents();
        setIcon();
        this.setLocation(500, 100);
    }
    
    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/icon.png")));
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        pan_main = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txt_Password = new javax.swing.JPasswordField();
        txt_RePassword = new javax.swing.JPasswordField();
        btn_Change = new javax.swing.JButton();
        btn_Clear = new javax.swing.JButton();

        jTextField1.setText("jTextField1");

        setTitle("Thay đổi mật khẩu");
        setResizable(false);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel2.setText("Mật khẩu mới");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel3.setText("Nhập lại mật khẩu");

        txt_Password.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txt_PasswordMouseClicked(evt);
            }
        });
        txt_Password.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_PasswordKeyPressed(evt);
            }
        });

        txt_RePassword.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txt_RePasswordMouseClicked(evt);
            }
        });
        txt_RePassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_RePasswordKeyPressed(evt);
            }
        });

        btn_Change.setBackground(new java.awt.Color(204, 204, 255));
        btn_Change.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_Change.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/accept.png"))); // NOI18N
        btn_Change.setText("Thay Đổi");
        btn_Change.setBorder(null);
        btn_Change.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ChangeActionPerformed(evt);
            }
        });
        btn_Change.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btn_ChangeKeyPressed(evt);
            }
        });

        btn_Clear.setBackground(new java.awt.Color(204, 204, 255));
        btn_Clear.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_Clear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete_friend.png"))); // NOI18N
        btn_Clear.setText("Hủy Bỏ");
        btn_Clear.setBorder(null);
        btn_Clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ClearActionPerformed(evt);
            }
        });
        btn_Clear.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btn_ClearKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout pan_mainLayout = new javax.swing.GroupLayout(pan_main);
        pan_main.setLayout(pan_mainLayout);
        pan_mainLayout.setHorizontalGroup(
            pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pan_mainLayout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addComponent(btn_Change, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btn_Clear, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52))
            .addGroup(pan_mainLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_Password, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txt_RePassword, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31))
        );
        pan_mainLayout.setVerticalGroup(
            pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pan_mainLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_Password, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txt_RePassword, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Change, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Clear, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pan_main, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pan_main, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_ClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ClearActionPerformed
        txt_Password.setText(null);
        txt_RePassword.setText(null);
    }//GEN-LAST:event_btn_ClearActionPerformed

    private void txt_PasswordMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txt_PasswordMouseClicked
        txt_Password.setText(null);
    }//GEN-LAST:event_txt_PasswordMouseClicked
    private void txt_RePasswordMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txt_RePasswordMouseClicked
        txt_RePassword.setText(null);
    }//GEN-LAST:event_txt_RePasswordMouseClicked

    private void btn_ChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ChangeActionPerformed
       if(txt_Password.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Chưa nhập mật khẩu","Lỗi thay đổi mật khẩu", JOptionPane.ERROR_MESSAGE);
            txt_Password.requestFocus();
        }
        else if(txt_RePassword.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Chưa nhập lại mật khẩu","Lỗi thay đổi mật khẩu", JOptionPane.ERROR_MESSAGE);
            txt_RePassword.requestFocus();
        }
        else if(!txt_Password.getText().equals(txt_RePassword.getText())){
            JOptionPane.showMessageDialog(null, "Mật khẩu nhập lại không đúng","Lỗi thay đổi mật khẩu", JOptionPane.ERROR_MESSAGE);
            txt_RePassword.setText(null);
            txt_RePassword.requestFocus();
        }
        else{
            Connection.sendData(Command.CHANGE_PASS_REQUEST+username+((char)001)+txt_Password.getText());
        }
    }//GEN-LAST:event_btn_ChangeActionPerformed

    private void txt_RePasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_RePasswordKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            btn_ChangeActionPerformed(null);
        }
    }//GEN-LAST:event_txt_RePasswordKeyPressed

private void txt_PasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_PasswordKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        txt_RePassword.requestFocusInWindow();
    }
}//GEN-LAST:event_txt_PasswordKeyPressed

    private void btn_ChangeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btn_ChangeKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            btn_ChangeActionPerformed(null);
        }
    }//GEN-LAST:event_btn_ChangeKeyPressed

    private void btn_ClearKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btn_ClearKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            txt_Password.setText(null);
            txt_RePassword.setText(null);
        }
    }//GEN-LAST:event_btn_ClearKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Change;
    private javax.swing.JButton btn_Clear;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel pan_main;
    private javax.swing.JPasswordField txt_Password;
    private javax.swing.JPasswordField txt_RePassword;
    // End of variables declaration//GEN-END:variables
}
